using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using METODESA.Server.Data.Exceptions;
using METODESA.Server.Data.METODESA.Entity;
using METODESA.Server.Data.METODESA.Nhibernate;
using NHibernate;

namespace Metodesa.Logic
{
    public class ProveedorLogic
    {

        public static IList<Proveedor> processSearch(string search, string criteria)
        {
            if (criteria == "" || search == "")
            {
                return ProveedorDao.getAllProveedor();
            }
            else
            {

                switch (criteria)
                {
                    case "Ci": return ProveedorDao.GetProveedorByCedula(search);

                    case "Name": return ProveedorDao.GetProveedorByName(search);

                }

            }

            return null;
        }


        public static void add (Proveedor proveedor)
        {
            ITransaction myTransaction = null;
            try
            {
                ISession mySession = NHibernateHelper.GetCurrentSession();
                myTransaction = mySession.BeginTransaction();
                myTransaction.Begin();
                ProveedorDao.saveSimple(proveedor);
                
                foreach (Telefono tel in proveedor.telefono)
                {
                    ProveedorDao.saveSimple(tel);
                }
                myTransaction.Commit();
            }
            catch(HibernateException e)
            {
                if (myTransaction != null)
                    myTransaction.Rollback();
                NHibernateHelper.GetCurrentSession().Clear(); // Quitamos el query que nos dio error
                throw new EntityCouldntBeSaved("El producto no se ha podido guardar " + e.Message);
                
            }
            catch (Exception e)
            {
                if (myTransaction != null)
                    myTransaction.Rollback();
            }

        }

        internal static void update(Proveedor proveedor){

            ITransaction myTransaction = null;
            try
            {
                ISession mySession = NHibernateHelper.GetCurrentSession();
                myTransaction = mySession.BeginTransaction();
                myTransaction.Begin();
                ProveedorDao.updateSimple(proveedor);

                foreach (Telefono tel in proveedor.telefono)
                {
                    ProveedorDao.updateSimple(tel);
                }
                myTransaction.Commit();
            }
            catch (HibernateException e)
            {
                if (myTransaction != null)
                    myTransaction.Rollback();
                NHibernateHelper.GetCurrentSession().Clear(); // Quitamos el query que nos dio error
                throw new EntityCouldntBeSaved("El proveedor no se ha podido guardar " + e.Message);

            }
            catch (Exception e)
            {
                if (myTransaction != null)
                    myTransaction.Rollback();
            }
        
            
        }

        internal static void delete(int id)
        {

            ITransaction myTransaction = null;
            try
            {
                ISession mySession = NHibernateHelper.GetCurrentSession();
                myTransaction = mySession.BeginTransaction();
                myTransaction.Begin();
                Proveedor proveedor = ProveedorDao.getProveedorById(id);
                ProveedorDao.deleteSimple(proveedor);

                myTransaction.Commit();
            }
            catch (HibernateException e)
            {
                if (myTransaction != null)
                    myTransaction.Rollback();
                NHibernateHelper.GetCurrentSession().Clear(); // Quitamos el query que nos dio error
                throw new EntityCouldntBeSaved("El proveedor no se ha podido eliminar " + e.Message);

            }
            catch (Exception e)
            {
                if (myTransaction != null)
                    myTransaction.Rollback();
            }


        }


    }
}
